home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / RESOURCES / CH22 / item.cs next >
Text File  |  2006-09-21  |  7KB  |  239 lines

  1. //============================================================================
  2. // control/misc/items.cs
  3. //
  4. //
  5. //  Copyright (c) 2003 by Kenneth C.  Finney.
  6. //============================================================================
  7.  
  8. // These scripts make use of dynamic attribute values on Item datablocks,
  9. // these are as follows:
  10. //
  11. //    maxInventory      Max inventory per object (100 bullets per box, etc.)
  12. //    pickupName        Name to display when client pickups item
  13. //
  14. // Item objects can have:
  15. //
  16. //    count             The # of inventory items in the object.  This
  17. //                      defaults to maxInventory if not set.
  18.  
  19. // Respawntime is the amount of time it takes for a static "auto-respawn"
  20. // object, such as an ammo box or weapon, to re-appear after it's been
  21. // picked up.  Any item marked as "static" is automaticlly respawned.
  22. $Item::RespawnTime = 20 * 1000;
  23.  
  24. // Poptime represents how long dynamic items (those that are thrown or
  25. // dropped) will last in the world before being deleted.
  26. $Item::PopTime = 10 * 1000;
  27.  
  28.  
  29. //-----------------------------------------------------------------------------
  30. // ItemData base class methods used by all items
  31. //-----------------------------------------------------------------------------
  32.  
  33. //-----------------------------------------------------------------------------
  34.  
  35. function Item::respawn(%this)
  36. {
  37.    // This method is used to respawn static ammo and weapon items
  38.    // and is usually called when the item is picked up.
  39.    // Instant fade...
  40.    %this.startFade(0, 0, true);
  41.    %this.setHidden(true);
  42.  
  43.    // Shedule a reapearance
  44.    %this.schedule($Item::RespawnTime, "hide", false);
  45.    %this.schedule($Item::RespawnTime + 100, "startFade", 1000, 0, false);
  46. }
  47.  
  48. function Item::schedulePop(%this)
  49. {
  50.    // This method deletes the object after a default duration. Dynamic
  51.    // items such as thrown or drop weapons are usually popped to avoid
  52.    // world clutter.
  53.    %this.schedule($Item::PopTime - 1000, "startFade", 1000, 0, true);
  54.    %this.schedule($Item::PopTime, "delete");
  55. }
  56.  
  57.  
  58. //-----------------------------------------------------------------------------
  59. // Callbacks to hook items into the inventory system
  60.  
  61. function ItemData::onThrow(%this,%user,%amount)
  62. {
  63.    // Remove the object from the inventory
  64.    if (%amount $= "")
  65.       %amount = 1;
  66.    if (%this.maxInventory !$= "")
  67.       if (%amount > %this.maxInventory)
  68.          %amount = %this.maxInventory;
  69.    if (!%amount)
  70.       return 0;
  71.    %user.decInventory(%this,%amount);
  72.  
  73.    // Construct the actual object in the world, and add it to
  74.    // the mission group so it's cleaned up when the mission is
  75.    // done.  The object is given a random z rotation.
  76.    %obj = new Item() {
  77.       datablock = %this;
  78.       rotation = "0 0 1 " @ (getRandom() * 360);
  79.       count = %amount;
  80.    };
  81.    MissionGroup.add(%obj);
  82.    %obj.schedulePop();
  83.    return %obj;
  84. }
  85.  
  86. function ItemData::onPickup(%this,%obj,%user,%amount)
  87. {
  88.    // Add it to the inventory, this currently ignores the request
  89.    // amount, you get what you get.  If the object doesn't have
  90.    // a count or the datablock doesn't have maxIventory set, the
  91.    // object cannot be picked up.
  92.    %count = %obj.count;
  93.  
  94.    if (%count $= "")
  95.       if (%this.maxInventory !$= "") {
  96.          if (!(%count = %this.maxInventory))
  97.             return;
  98.       }
  99.       else
  100.          %count = 1;
  101.    %user.incInventory(%this,%count);
  102.  
  103.    // Inform the client what they got.
  104.    if (%user.client)
  105.    {
  106.       messageClient(%user.client, 'MsgItemPickup', '\c0You picked up %1', %this.pickupName);
  107.       %user.client.money += %this.value;
  108.       %user.client.DoScore();
  109.    }
  110.  
  111.    // If the item is a static respawn item, then go ahead and
  112.    // respawn it, otherwise remove it from the world.
  113.    // Anything not taken up by inventory is lost.
  114.    if (%obj.isStatic())
  115.       %obj.respawn();
  116.    else
  117.       %obj.delete();
  118.    return true;
  119. }
  120.  
  121. //-----------------------------------------------------------------------------
  122. // Hook into the mission editor.
  123.  
  124. function ItemData::create(%data)
  125. {
  126.    // The mission editor invokes this method when it wants to create
  127.    // an object of the given datablock type.  For the mission editor
  128.    // we always create "static" re-spawnable rotating objects.
  129.    %obj = new Item() {
  130.       dataBlock = %data;
  131.       static = true;
  132.       rotate = true;
  133.    };
  134.    return %obj;
  135. }
  136.  
  137. datablock ItemData(Copper)
  138. {
  139.    // Mission editor category, this datablock will show up in the
  140.    // specified category under the "shapes" root category.
  141.    category = "Coins";
  142.  
  143.    // Basic Item properties
  144.    shapeFile = "~/data/models/items/kash1.dts";
  145.    mass = 0.7;
  146.    friction = 0.8;
  147.    elasticity = 0.3;
  148.  
  149.  
  150.    respawnTime = 30 * 60000;
  151.    salvageTime = 15 * 60000;
  152.    // Dynamic properties defined by the scripts
  153.    pickupName = "a copper coin";
  154.    value = 1;
  155. };
  156.  
  157. datablock ItemData(Silver)
  158. {
  159.    // Mission editor category, this datablock will show up in the
  160.    // specified category under the "shapes" root category.
  161.    category = "Coins";
  162.  
  163.    // Basic Item properties
  164.    shapeFile = "~/data/models/items/kash100.dts";
  165.    mass = 0.7;
  166.    friction = 0.8;
  167.    elasticity = 0.3;
  168.  
  169.  
  170.    respawnTime = 30 * 60000;
  171.    salvageTime = 15 * 60000;
  172.    // Dynamic properties defined by the scripts
  173.    pickupName = "a silver coin";
  174.    value = 100;
  175. };
  176.  
  177. datablock ItemData(Gold)
  178. {
  179.    // Mission editor category, this datablock will show up in the
  180.    // specified category under the "shapes" root category.
  181.    category = "Coins";
  182.  
  183.    // Basic Item properties
  184.    shapeFile = "~/data/models/items/kash1000.dts";
  185.    mass = 0.7;
  186.    friction = 0.8;
  187.    elasticity = 0.3;
  188.  
  189.  
  190.    respawnTime = 30 * 60000;
  191.    salvageTime = 15 * 60000;
  192.    // Dynamic properties defined by the scripts
  193.    pickupName = "a gold coin";
  194.    value = 1000;
  195. };
  196.  
  197.  
  198. //-----------------------------------------------------------------------------
  199. // Health Patchs cannot be picked up and are not meant to be added to
  200. // inventory.  Health is applied automatically when an objects collides
  201. // with a patch.
  202. //-----------------------------------------------------------------------------
  203.  
  204. datablock ItemData(FirstAidKit)
  205. {
  206.    // Mission editor category, this datablock will show up in the
  207.    // specified category under the "shapes" root category.
  208.    category = "Health";
  209.  
  210.    // Basic Item properties
  211.    shapeFile = "~/data/models/items/healthPatch.dts";
  212.    mass = 1;
  213.    friction = 1;
  214.    elasticity = 0.3;
  215.  
  216.    respawnTime = 600000;
  217.    // Dynamic properties defined by the scripts
  218.    repairAmount = 200;
  219.    maxInventory = 0; // No pickup or throw
  220. };
  221.  
  222. function FirstAidKit::onCollision(%this,%obj,%col)
  223. {
  224.    // Apply health to colliding object if it needs it.
  225.    // Works for all shapebase objects.
  226.    if (%col.getDamageLevel() != 0 && %col.getState() !$= "Dead" )
  227.    {
  228.       %col.applyRepair(%this.repairAmount);
  229.       %obj.respawn();
  230.       if (%col.client)
  231.       {
  232.          messageClient
  233.               (%col.client,'MSG_Treatment','\c2Medical treatment applied');
  234.       }
  235.    }
  236. }
  237.  
  238.  
  239.